﻿#include "wordsinputdialog.h"
#include "ui_wordsinputdialog.h"

WordsInputDialog::WordsInputDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WordsInputDialog)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明

    m_buttonWordList.clear();
    m_buttonWordList.append(ui->buttonLatterQ);
    m_buttonWordList.append(ui->buttonLatterW);
    m_buttonWordList.append(ui->buttonLatterE);
    m_buttonWordList.append(ui->buttonLatterR);
    m_buttonWordList.append(ui->buttonLatterT);
    m_buttonWordList.append(ui->buttonLatterY);
    m_buttonWordList.append(ui->buttonLatterU);
    m_buttonWordList.append(ui->buttonLatterI);
    m_buttonWordList.append(ui->buttonLatterO);
    m_buttonWordList.append(ui->buttonLatterP);
    m_buttonWordList.append(ui->buttonLatterA);
    m_buttonWordList.append(ui->buttonLatterS);
    m_buttonWordList.append(ui->buttonLatterD);
    m_buttonWordList.append(ui->buttonLatterF);
    m_buttonWordList.append(ui->buttonLatterG);
    m_buttonWordList.append(ui->buttonLatterH);
    m_buttonWordList.append(ui->buttonLatterJ);
    m_buttonWordList.append(ui->buttonLatterK);
    m_buttonWordList.append(ui->buttonLatterL);
    m_buttonWordList.append(ui->buttonLatterZ);
    m_buttonWordList.append(ui->buttonLatterX);
    m_buttonWordList.append(ui->buttonLatterC);
    m_buttonWordList.append(ui->buttonLatterV);
    m_buttonWordList.append(ui->buttonLatterB);
    m_buttonWordList.append(ui->buttonLatterN);
    m_buttonWordList.append(ui->buttonLatterM);

    for(int var = 0; var < m_buttonWordList.size(); var++)
    {
        connect(m_buttonWordList.at(var),
                SIGNAL(clicked()),
                this,
                SLOT(inputBtnClick())
                );
    }

    m_buttonNumberList.clear();
    m_buttonNumberList.append(ui->buttonNum1);
    m_buttonNumberList.append(ui->buttonNum2);
    m_buttonNumberList.append(ui->buttonNum3);
    m_buttonNumberList.append(ui->buttonNum4);
    m_buttonNumberList.append(ui->buttonNum5);
    m_buttonNumberList.append(ui->buttonNum6);
    m_buttonNumberList.append(ui->buttonNum7);
    m_buttonNumberList.append(ui->buttonNum8);
    m_buttonNumberList.append(ui->buttonNum9);
    m_buttonNumberList.append(ui->buttonNum0);
    for(int var = 0; var < m_buttonNumberList.size(); var++)
    {
        connect(m_buttonNumberList.at(var),
                SIGNAL(clicked()),
                this,
                SLOT(inputBtnClick())
                );
    }

    m_shift = 1;
    m_inputStr.clear();
    ui->labelNameNewWords->clear();
    ui->labelNameOldWords->clear();
    ui->buttonOk->setEnabled(false);

    initControl();
}

WordsInputDialog::~WordsInputDialog()
{
    delete ui;
}

void WordsInputDialog::setInputStr(QString str)
{
    m_inputStr = m_inputStr + str;
    ui->labelNameNewWords->setText(m_inputStr);
    if(m_inputStr.length() > 0)
    {
        ui->buttonOk->setEnabled(true);
    }
}

//初始化窗体控件，包括位置、尺寸、样式
void WordsInputDialog::initControl()
{
    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为任意分辨率
void WordsInputDialog::initResolution()
{
    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

    this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    this->move((GLB_SCR_WIGHT*factoryX-this->width())/2+g_mainWidgetPos.x(),(GLB_SCR_HEIGHT*factoryY-this->height())/2+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0*factoryX,0,GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    ui->labelTitle->setGeometry(50*factoryX,55*factoryY,GLB_SCR_WIGHT*factoryX,48*factoryY);

    ui->labelName->setGeometry(65*factoryX,148*factoryY,95*factoryX,33*factoryY);
    ui->labelNameNewWords->setGeometry(194*factoryX,143*factoryY,554*factoryX,44*factoryY);
    ui->labelNameOldWords->setGeometry(575*factoryX,147*factoryY,200*factoryX,44*factoryY);

    for(int i = 0; i < m_buttonNumberList.size(); i++)
    {
        m_buttonNumberList[i]->setGeometry((60+i*91)*factoryX,233*factoryY,67*factoryX,40*factoryY);
    }

    int x = 0;
    int y = 0;
    int idx = 0;
    for(int i = 0; i < m_buttonWordList.size(); i++)
    {
        if(m_buttonWordList[i]->text().toUpper() == "Q")
        {
           x = 60;
           y = 295;
           idx = i;
        }
        if(m_buttonWordList[i]->text().toUpper() == "A")
        {
           x =97;
           y = 353;
           idx = 0;
        }
        if(m_buttonWordList[i]->text().toUpper() == "Z")
        {
           x = 189;
           y = 414;
           idx = 0;
        }
        m_buttonWordList[i]->setGeometry((x+idx*91)*factoryX,y*factoryY,67*factoryX,40*factoryY);
        idx++;
    }

    ui->buttonShift->setGeometry(60*factoryX,414*factoryY,107*factoryX,40*factoryY);
    ui->buttonBack->setGeometry(827*factoryX,414*factoryY,107*factoryX,40*factoryY);
    ui->frameTwo->setGeometry(725*factoryX,472*factoryY,321*factoryX,51*factoryY);
    ui->buttonOk->setGeometry(0*factoryX,0*factoryY,107*factoryX,40*factoryY);
    ui->buttonCancel->setGeometry(135*factoryX,0*factoryY,107*factoryX,40*factoryY);
}

//初始化窗体控件样式
void WordsInputDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    for(int i = 0; i < m_buttonWordList.size(); i++)
    {
        m_buttonWordList[i]->setStyleSheet(numButtonStyle());
        m_buttonWordList[i]->setFont(fontSize_M());
    }

    for(int i = 0; i < m_buttonNumberList.size(); i++)
    {
        m_buttonNumberList[i]->setStyleSheet(numButtonStyle());
        m_buttonNumberList[i]->setFont(fontSize_M());
    }

    ui->frameBack->setStyleSheet(transparentStyle());
    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));

    ui->labelName->setStyleSheet(titleTextColour());
    ui->labelName->setFont(fontSize_M());
    ui->labelName->setVisible(false);
//    ui->labelName->setAlignment(Qt::AlignRight);
//    ui->labelName->setAlignment(Qt::AlignVCenter);

    ui->labelTitle->setStyleSheet(titleTextColour());
    ui->labelTitle->setFont(fontSize_M());
    ui->labelTitle->setAlignment(Qt::AlignLeft);
    ui->labelTitle->setAlignment(Qt::AlignVCenter);

    ui->labelNameNewWords->setStyleSheet(warnTextColour());
    ui->labelNameNewWords->setFont(fontSize_M());
    ui->labelNameNewWords->setAlignment(Qt::AlignLeft);
    ui->labelNameNewWords->setAlignment(Qt::AlignVCenter);

    ui->labelNameOldWords->setStyleSheet(noteTextColour());
    ui->labelNameOldWords->setFont(fontSize_M());
    ui->labelNameOldWords->setAlignment(Qt::AlignLeft);
    ui->labelNameOldWords->setAlignment(Qt::AlignVCenter);

    ui->buttonShift->setStyleSheet(numButtonStyle());
    ui->buttonShift->setFont(fontSize_M());

    ui->buttonBack->setStyleSheet(numButtonStyle());
    ui->buttonBack->setFont(fontSize_M());

    ui->frameTwo->setStyleSheet(transparentStyle());

    ui->buttonOk->setStyleSheet(confirmIconStyle());
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet("buttonOk"),12);
    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet("buttonCancel"),12);

}

void WordsInputDialog::setTitleStr(QString str)
{
    ui->labelTitle->setText(str);
}

void WordsInputDialog::setOldWordsStr(QString str)
{
    ui->labelNameOldWords->setText(str);
}

void WordsInputDialog::clean()
{
    m_inputStr.clear();
    ui->labelNameNewWords->clear();
    ui->buttonOk->setEnabled(false);
}

void WordsInputDialog::wifiClean()
{
    m_inputStr.clear();
    ui->labelNameNewWords->clear();
    ui->buttonOk->setEnabled(true);
}

QString WordsInputDialog::getInputStr()
{
    return m_inputStr;
}

void WordsInputDialog::inputBtnClick()
{
    QPushButton *button = (QPushButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }
    QString str = button->text();
    setInputStr(str);
}

void WordsInputDialog::on_buttonShift_clicked()
{
    m_shift = -1 * m_shift;

    for(int i = 0; i < m_buttonWordList.size(); i++)
    {
        if(m_shift == -1)
        {
            QString lostr=m_buttonWordList[i]->text().toLower();
            m_buttonWordList[i]->setText(lostr);
        }
        else if(m_shift == 1)
        {
            QString upstr=m_buttonWordList[i]->text().toUpper();
            m_buttonWordList[i]->setText(upstr);
        }
    }
}

void WordsInputDialog::on_buttonBack_clicked()
{
    if(m_inputStr.length() <= 0)
    {
        return;
    }

    m_inputStr = m_inputStr.remove(m_inputStr.length()-1,m_inputStr.length());    // 删除最后一位字符
    ui->labelNameNewWords->setText(m_inputStr);

    if(m_inputStr.length() <= 0)
    {
        ui->buttonOk->setEnabled(false);
    }
}

void WordsInputDialog::on_buttonOk_clicked()
{
    done(1);
}

void WordsInputDialog::on_buttonCancel_clicked()
{
    clean();
    done(0);
}
